package com.ygo.feihua.fragment;
import android.content.*;
import android.graphics.*;
import android.os.*;
import android.support.v4.app.*;
import android.support.v4.util.*;
import android.view.*;
import android.view.View.*;
import android.widget.*;
import android.widget.AdapterView.*;
import com.bumptech.glide.*;
import com.bumptech.glide.load.engine.*;
import com.bumptech.glide.request.target.*;
import java.io.*;
import java.util.*;

import android.view.View.OnClickListener;
import com.cc.chioceFileListView.*;
import com.ygo.feihua.util.*;
import com.bumptech.glide.request.transition.*;
import com.yuyh.library.imgsel.*;
import com.yuyh.library.imgsel.ui.*;
import com.ygo.feihua.R;
import com.ygo.feihua.*;

public class FragmentZsYulantu extends Fragment
{
	private List<Map<String, Object>> data;  

	private Adaptertp adptp;
	private ListView fz_list;
	Gongju gj;
	private DialogUtils du;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
	{
		View v=inflater.inflate(R.layout.fragment_zs_yulantu,null);
		
		fz_list=(ListView) v.findViewById(R.id.fz_list);
		data=new ArrayList<Map<String,Object>>();

		gj=Gongju.getdx(getActivity());
		du=DialogUtils.getdx(getActivity());
        /* 
         * 载入默认图片添加图片加号 
         * 通过适配器实现 
         * SimpleAdapter参数imageItem为数据源 R.layout.griditem_addpic为布局 
         */  
        //获取资源图片加号  
        Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.sc_yl);  
        data = new ArrayList<Map<String, Object>>();  
        Map<String, Object> map = new ArrayMap<String, Object>();  
        map.put("image", bmp);  
		map.put("path","");
        data.add(map);  
        adptp=new Adaptertp();
        fz_list.setAdapter(adptp);  
		
		/* 
         * 监听GridView点击事件 
         * 报错:该函数必须抽象方法 故需要手动导入import android.view.View; 
         */  
        fz_list.setOnItemClickListener(new OnItemClickListener() {  
				@Override  
				public void onItemClick(AdapterView<?> parent, View v, int position, long id)  
				{  
					if( data.size() == 9) {
						gj.show("最多只能选择8张图片哦~");
					}  
					else if(position==data.size()-1) { 
						/*Intent i = new Intent(Intent.ACTION_PICK,MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

						 i.setType("image/*");
						 startActivityForResult(i, 234);*/

						ISNav.getInstance().toListActivity(getActivity(), Gongju.getPicConfig(4), 233);
						
						

					}  
					else {  
						dialog(position); 
					}  
				}  
			});    
		
		
		final ZhutiShangchuan zs=(ZhutiShangchuan) getActivity();
		Button zs_next=zs.sc_sc;
		zs_next.setText("下一步");
		zs_next.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View p1)
				{
					final String[] ss=new String[data.size()-1];
					for(int i=0;i<data.size()-1;i++){
						ss[i]=(String)data.get(i).get("path");
					}
					if(ss.length==0){
						gj.show("至少上传一张预览图");
					}else{
						
						for(String s1:ss){
							zs.zt_jt.add(s1);
						}
						
					zs.fragmentManager.beginTransaction().replace(R.id.sc_frage, new FragmentZsZip()).commit();

					}
					// TODO: Implement this method
				}
			});
		
		// TODO: Implement this method
		return v;
	}

	
	
	
	
	
	/* 
     * Dialog对话框提示用户删除操作 
     * position为删除图片位置 
     */  
    private void dialog(final int position) {  


		View[] v=du.dialogt(false,"","确认移除已添加图片？");
		Button b1,b2;
		b1=(Button) v[0];
		b2=(Button) v[1];

		b1.setText("取消");
		b2.setText("确定");
		b1.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View p1)
				{
					du.dis();
					// TODO: Implement this method
				}
			});
		b2.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View p1)
				{
					adptp.del(position);
					du.dis();
					// TODO: Implement this method
				}
			});

    }
	
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		// TODO: Implement this method
		super.onActivityResult(requestCode, resultCode, data);


		if (resultCode == getActivity().RESULT_OK && requestCode == 233)
		{
			if (data != null)
			{
				ArrayList<String> photos = data.getStringArrayListExtra(ISListActivity.INTENT_RESULT);
				for (final String ss:photos)
				{

					Glide.with(this)
						.asBitmap()
						.load(new File(ss))
						.apply(Gongju.glideOption)
						.into((SimpleTarget)new SimpleTarget<Bitmap>(){

								  @Override
								  public void onResourceReady(Bitmap p1, Transition<? super Bitmap> p2)
								  {
									  Map<String ,Object> map=new ArrayMap<String,Object>();
									  map.put("path", ss);
									  map.put("image", p1);
									  adptp.add(map);
									  // TODO: Implement this method
								  }
							  });
				}
			}
		}

	}  


	
	private class Adaptertp extends BaseAdapter
	{

		Zujian zujian;
		class Zujian{
			ImageView ii_image;
		}

		public void del(int po){
			data.remove(po);
			notifyDataSetChanged();
		}
		public void add(Map<String,Object> map){
			data.add(data.size()-1,map);
			notifyDataSetChanged();
		}

		@Override
		public int getCount()
		{
			// TODO: Implement this method
			return data.size();
		}

		@Override
		public Object getItem(int p1)
		{
			// TODO: Implement this method
			return data.get(p1);
		}

		@Override
		public long getItemId(int p1)
		{
			// TODO: Implement this method
			return p1;
		}

		@Override
		public View getView(int position, View p2, ViewGroup p3)
		{
			if(p2==null){
				zujian=new Zujian();
				p2=LayoutInflater.from(getActivity()).inflate(R.layout.item_imagem,null);
				zujian.ii_image=(ImageView) p2.findViewById(R.id.ii_image);
				p2.setTag(zujian);
			}else{
				zujian=(Zujian)p2.getTag();
			}

			//zujian.ii_image.setImageBitmap((Bitmap)data.get(position).get("image"));

			Glide.with(getActivity()).load((String)data.get(position).get("path")).into(zujian.ii_image);
			
			
			// TODO: Implement this method
			return p2;
		}
	}
	
	
	
}
